package cn.hxzy.zyicon.controller;

import cn.hxzy.zyicon.entity.Resource;
import cn.hxzy.zyicon.entity.Role;
import cn.hxzy.zyicon.entity.User;
import cn.hxzy.zyicon.entity.po.ResourcePO;
import cn.hxzy.zyicon.entity.po.UserPO;
import cn.hxzy.zyicon.entity.vo.ReturnBean;
import cn.hxzy.zyicon.service.ResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @PostMapping("list")
    public ReturnBean list() {
        List<Resource> all = resourceService.findAll();
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }
    @PostMapping("add")
    public ReturnBean add(Resource resource){
        resourceService.save(resource);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("resourceList1")
    public ReturnBean resourceList1() {
        List<Resource> all =resourceService.findAll();
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,all);
    }
    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        resourceService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("update")
    public ReturnBean update(ResourcePO resourcePO) {
        Resource resource=new Resource();
        BeanUtils.copyProperties(resourcePO,resource);
        resourceService.update(resource);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Resource byId = resourceService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,byId);
    }
}
